<?php

	include_once("$config[webroot]/includes/page_utf_class.php");
	include_once($config['webroot']."/module/activity/includes/plugin_special_class.php");

	if($_GET['operation']=="add" or $_GET['operation']=="edit")
	{
		if($_GET['act']=='delfloor' && $_GET['id'] && is_numeric($_GET['id'])){//删除楼层
			$sql = "delete from ".SPECIALPRODUCTFLOOR." where id={$_GET['id']}";
			$db->query($sql);
		}
		if($_POST['act'])
		{	
			unset($_GET['s']);
			unset($_GET['m']);
			unset($_GET['operation']);
			//添加活动
			$_POST['stime'].=(' '.$_POST['startHour'].':'.$_POST['startMinute'].':00');
			$_POST['etime'].=(' '.$_POST['endHour'].':'.$_POST['endMinute'].':00');
			
			if($_POST['type']){
				$_POST['type_config']=$_POST['type_config_'.$_POST['type']];
			}else{
				$_POST['type_config']='';
			}
			if(is_array($_POST['type_config'])){
				$_tmp=$_sorttmp=array();
				foreach ($_POST['type_config'] as $item){
					if (is_numeric($item[0])&&is_numeric($item[1])){
						$_tmp[$item[0]]=$item[1];
					}
				}
				ksort($_tmp);
				foreach ($_tmp as $key=>$item){
					$_sorttmp[]=$key.'-'.$item;
				}
				$_POST['type_config']=implode(',', $_sorttmp);
			}
/* 			var_dump($_POST);
			exit; */
			if($_POST["act"]=='save')
			{
				$activity=new special();
				$activity->add_special();
			}
			//修改活动
			if($_POST["act"]=='edit' and is_numeric($_POST['id']))
			{
				$activity=new special();
				$activity->edit_special();
				
				if($_POST['new_floor']){//新加楼层
					$values = "";
					foreach ($_POST['new_floor'] as $key=>$f)
					{
						if($f){
							$values .= "('{$f}',{$_POST['id']},'{$_POST['new_fdisplayorder'][$key]}','{$_POST['new_fbgcolor'][$key]}'),";
						}
					}
					if($values!=''){
						$sql = "INSERT INTO ".SPECIALPRODUCTFLOOR." (`title`,`special_id`,`displayorder`,`bgcolor`) VALUES ".rtrim($values,",");
						$db->query($sql);
					}
				}

				if($_POST['floor']){//修改楼层信息
					foreach ($_POST['floor'] as $fk=>$fv)
					{
						$db->query("update ".SPECIALPRODUCTFLOOR." set title='{$fv}',displayorder={$_POST['fdisplayorder'][$fk]},bgcolor='{$_POST['fbgcolor'][$fk]}' where id={$fk}");
					}
				}
				
				unset($_GET['editid']);
			}
			$getstr=implode('&',convert($_GET));
			msg("?m=activity&s=special.php&$getstr");
		}
		//活动信息
		if($_GET['editid'] and is_numeric($_GET['editid']))
		{
			$sql="select * from ".SPECIAL." where id='$_GET[editid]'";
			$db->query($sql);
			$de=$db->fetchRow();
			$de['type_config_'.$de['type']]=array_map(function($v){return explode("-",$v);}, explode(',', $de['type_config']));
			
			$sql = "select id,title,displayorder,bgcolor from ".SPECIALPRODUCTFLOOR." where special_id={$_GET['editid']} order by displayorder asc";
			$db->query($sql);
			$de['floors'] = $db->getRows();
		}
	}
	
	
	else
	{
		//删除活动
		if($_GET['delid'])
		{
			$sql="update ".SPECIAL."  set status=-9 where id='$_GET[delid]'";
			$db->query($sql);
			$sql="delete from ".SPECIALPRODUCT."  where activity_id='$_GET[delid]'";
			$db->query($sql);
			unset($_GET['delid']);
			$getstr=implode('&',convert($_GET));
			msg("?m=activity&s=special.php&$getstr");
		}
		if($_POST['act']=='op')
		{
			if(is_array($_POST['chk']))
			{
				$id=implode(",",$_POST['chk']);
				$sql="update ".SPECIAL."  set status=-9 where id in ($id)";
				$db->query($sql);
				$sql="delete from ".SPECIALPRODUCT." where activity_id in ($id)";
				$db->query($sql);
			}
			if($_POST['displayorder'])
			{
				foreach($_POST['displayorder'] as $key=>$list)
				{
					$db->query("update ".SPECIAL." set displayorder='$list' where id='$key'");		
				}
			}
			msg("?m=activity&s=special.php");
		}
		//获取活动
		$where = "";
		if(($_GET['statu']!="") && ($_GET['statu']>=0)){
		    $where .=" and status='$_GET[statu]'";
		}else{
			$where .=" and status>-9";
		}
		if($_GET['sdate']!='' && $_GET['edate']!='' && (strtotime($_GET['sdate'])<=strtotime($_GET['edate']))){
		    $where .=" and start_time>='".strtotime($_GET['sdate'])."' and end_time<='".strtotime($_GET['edate'])."'";
		}else{
		    if($_GET['sdate']!='' && $_GET['edate']==''){
		        $where .=" and start_time>='".strtotime($_GET['sdate'])."'";
		    }
		    if($_GET['sdate']=='' && $_GET['edate']!=''){
		        $where .=" and end_time<='".strtotime($_GET['edate'])."'";
		    }
		}
		if($_GET['is_start']!=''){
			$now_time = time();
			switch ($_GET['is_start']){
				case -1:
					$where .= " and end_time<'".$now_time."'";break;
				case 1:
					$where .= " and start_time<='".$now_time."' and end_time>='".$now_time."'";break;
				case 2:
					$where .= " and start_time>'".$now_time."'";break;
				default:
					break;
			}
		}
		
		$sql="select * from ".SPECIAL." where 1".$where." order by displayorder asc,id desc";
		$page = new Page;
		$page->listRows=20;
		//分页
		if (!$page->__get('totalRows'))
		{
			$db->query($sql);
			$page->totalRows = $db->num_rows();
		}
		$sql .= "  limit ".$page->firstRow.",".$page->listRows;
		$db->query($sql);
		$de['list']=$db->getRows();
		
		$de['page']=$page->prompt();
	}
	//模板
	$sql="select * from ".SPECIALTEMPLATE." where status>0 order by id asc";
	$db->query($sql);
	$de['temp']=$db->getRows();
	
	$condTemp = $_GET;
	unset($condTemp['m']);
	unset($condTemp['s']);
	$cond = "";
	if(count($condTemp)){
		$cond = "&".http_build_query($condTemp);
	}
	$tpl->assign("cond",$cond);
	$tpl->assign("de",$de);
	$tpl->assign("config",$config);
	$tpl->display("special.htm");

?>